package com.seven.demo.java8;

import com.seven.demo.model.User;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author yinbenyang
 * @create 2018-11-14 16:59
 */
public class Test3 {

    public static void main(String[] args) {
        List<User> users = new ArrayList<>();
        users.add(new User(1,"张三",1,"2000-5-9"));
        users.add(new User(2,"李四",2,"2000-6-9"));
        users.add(new User(3,"王五",1,"2000-7-9"));
        users.add(new User(4,"赵六",2,"2000-8-9"));
        long c = users.stream().filter(user -> user.getSex() == 1).count();
        System.out.println(c);

        List<String> t1 = Stream.of("a","b","hello").collect(Collectors.toList());
        t1.stream().forEach(System.out::println);
        List<String> t2 = t1.stream().map(t -> t.toUpperCase()).collect(Collectors.toList());
        t2.stream().forEach(System.out::println);
        Stream<List<Integer>> listStream = Stream.of(Arrays.asList(1, 2), Arrays.asList(3, 4));
        listStream.flatMap(ls -> ls.stream()).forEach(System.out::println);
        int c1 = Stream.of(1,2,3).reduce(0,(acc,num) -> acc + num);
        System.out.println(c1);
        IntSummaryStatistics summaryStatistics = users.stream().mapToInt(u -> u.getId()).summaryStatistics();
        System.out.printf("max:%d min:%d ave:%f sum:%d count:%d",summaryStatistics.getMax(),summaryStatistics.getMin(),summaryStatistics.getAverage(),
                summaryStatistics.getSum(),summaryStatistics.getCount());

        List<Integer> t3 = IntStream.range(1,100).boxed().collect(Collectors.toList());
        System.out.println(t3.stream().mapToInt(Integer::intValue).summaryStatistics().getSum());
        Map<String, User> collect = users.stream().collect(Collectors.groupingBy(m -> m.getName() + "test"))
                .entrySet().stream().collect(Collectors.toMap((m -> m.getKey()), (m -> m.getValue().get(0))));
        collect.entrySet().stream().forEach(v -> {
            System.out.print(v.getKey() + "\t");
            System.out.println(v.getValue());
        });

    }
}
